package com.xbongbong.saas.model.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.dao.PaasFormPaasSubDataDao;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.PaasFormSubDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasFormSubDataEsModel;
import com.xbongbong.paas.elasticsearch.util.EsHelper;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.model.PaasFormPaasSubDataModel;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.wrap.IModel;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.pro.constant.BusinessConstant;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.pro.subform.pojo.dto.UpdateFormatSubDataDTO;
import com.xbongbong.saas.constant.SubFormTableNameConstant;
import com.xbongbong.saas.domain.entity.*;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.entity.AssembleSubDataEntity;
import com.xbongbong.saas.domain.entity.BomBillSubDataEntity;
import com.xbongbong.saas.domain.entity.ClueSubDataEntity;
import com.xbongbong.saas.domain.entity.CommunicatePlanSubDataEntity;
import com.xbongbong.saas.domain.entity.ContactSubDataEntity;
import com.xbongbong.saas.domain.entity.ContractSubDataEntity;
import com.xbongbong.saas.domain.entity.CreditFixedLimitSubDataEntity;
import com.xbongbong.saas.domain.entity.CreditTemporaryLimitSubDataEntity;
import com.xbongbong.saas.domain.entity.CustomerCommunicateSubDataEntity;
import com.xbongbong.saas.domain.entity.CustomerSubDataEntity;
import com.xbongbong.saas.domain.entity.FundTransferSubDataEntity;
import com.xbongbong.saas.domain.entity.InstockSubDataEntity;
import com.xbongbong.saas.domain.entity.InventorySubDataEntity;
import com.xbongbong.saas.domain.entity.InvoiceExpressSubDataEntity;
import com.xbongbong.saas.domain.entity.InvoiceSubDataEntity;
import com.xbongbong.saas.domain.entity.MarketActivitySubDataEntity;
import com.xbongbong.saas.domain.entity.OpportunitySubDataEntity;
import com.xbongbong.saas.domain.entity.OtherExpenseSubDataEntity;
import com.xbongbong.saas.domain.entity.OtherIncomeSubDataEntity;
import com.xbongbong.saas.domain.entity.OutstockSubDataEntity;
import com.xbongbong.saas.domain.entity.PayPlanSheetSubDataEntity;
import com.xbongbong.saas.domain.entity.PayPlanSubDataEntity;
import com.xbongbong.saas.domain.entity.PaymentSheetSubDataEntity;
import com.xbongbong.saas.domain.entity.PaymentSubDataEntity;
import com.xbongbong.saas.domain.entity.PaymentTaskSubDataEntity;
import com.xbongbong.saas.domain.entity.ProductSubDataEntity;
import com.xbongbong.saas.domain.entity.ProductionOrderSubDataEntity;
import com.xbongbong.saas.domain.entity.PurchaseInvoiceExpressSubDataEntity;
import com.xbongbong.saas.domain.entity.PurchaseInvoiceSubDataEntity;
import com.xbongbong.saas.domain.entity.PurchaseSubDataEntity;
import com.xbongbong.saas.domain.entity.QuotationSubDataEntity;
import com.xbongbong.saas.domain.entity.RefundSubDataEntity;
import com.xbongbong.saas.domain.entity.ReturnedPurchaseSubDataEntity;
import com.xbongbong.saas.domain.entity.SupplierCommunicateSubDataEntity;
import com.xbongbong.saas.domain.entity.SupplierContactSubDataEntity;
import com.xbongbong.saas.domain.entity.SupplierSubDataEntity;
import com.xbongbong.saas.domain.entity.TransferSubDataEntity;
import com.xbongbong.saas.domain.entity.WarehouseSubDataEntity;
import com.xbongbong.saas.domain.entity.WorkOrderSubDataEntity;
import com.xbongbong.saas.domain.entity.WorkReportSubDataEntity;
import com.xbongbong.saas.domain.entity.competitor.CompetitorSubDataEntity;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.enums.business.InvoiceEnum;
import com.xbongbong.saas.enums.business.PurchaseInvoiceEnum;
import com.xbongbong.saas.model.*;
import org.apache.commons.collections4.CollectionUtils;
import org.elasticsearch.action.support.WriteRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 子表单处理用model
 * @author 魏荣杰
 * @date 2020/9/16 16:36
 * @since
 * @version
 */
@Service("paasFormSubDataModel")
public class PaasFormSubDataModelImpl implements PaasFormSubDataModel {

    @Resource
    private PaasFormPaasSubDataModel paasFormPaasSubDataModel;
    @Resource
    private CustomerSubDataModel customerSubDataModel;
    @Resource
    private ContractSubDataModel contractSubDataModel;
    @Resource
    private QuotationSubDataModel quotationSubDataModel;
    @Resource
    private OutstockSubDataModel outstockSubDataModel;
    @Resource
    private ContactSubDataModel contactSubDataModel;
    @Resource
    private InvoiceSubDataModel invoiceSubDataModel;
    @Resource
    private InvoiceExpressSubDataModel invoiceExpressSubDataModel;
    @Resource
    private PurchaseInvoiceSubDataModel purchaseInvoiceSubDataModel;
    @Resource
    private PurchaseInvoiceExpressSubDataModel purchaseInvoiceExpressSubDataModel;
    @Resource
    private InstockSubDataModel instockSubDataModel;
    @Resource
    private AssembleSubDataModel assembleSubDataModel;
    @Resource
    private PaymentSubDataModel paymentSubDataModel;
    @Resource
    private PaymentSheetSubDataModel paymentSheetSubDataModel;
    @Resource
    private PaymentTaskSubDataModel paymentTaskSubDataModel;
    @Resource
    private PayPlanSheetSubDataModel payPlanSheetSubDataModel;
    @Resource
    private ProductionOrderSubDataModel productionOrderSubDataModel;
    @Resource
    private PayPlanSubDataModel payPlanSubDataModel;
    @Resource
    private CommunicatePlanSubDataModel communicatePlanSubDataModel;
    @Resource
    private OpportunitySubDataModel opportunitySubDataModel;
    @Resource
    private BomBillSubDataModel bomBillSubDataModel;
    @Resource
    private CustomerCommunicateSubDataModel customerCommunicateSubDataModel;
    @Resource
    private SupplierCommunicateSubDataModel supplierCommunicateSubDataModel;
    @Resource
    private WorkReportSubDataModel workReportSubDataModel;
    @Resource
    private PurchaseSubDataModel purchaseSubDataModel;
    @Resource
    private ReturnedPurchaseSubDataModel returnedPurchaseSubDataModel;
    @Resource
    private SupplierContactSubDataModel supplierContactSubDataModel;
    @Resource
    private SupplierSubDataModel supplierSubDataModel;
    @Resource
    private ProductSubDataModel productSubDataModel;
    @Resource
    private InventorySubDataModel inventorySubDataModel;
    @Resource
    private TransferSubDataModel transferSubDataModel;
    @Resource
    private WarehouseSubDataModel warehouseSubDataModel;
    @Resource
    private RefundSubDataModel refundSubDataModel;
    @Resource
    private WorkOrderSubDataModel workOrderSubDataModel;
    @Resource
    private OtherIncomeSubDataModel otherIncomeSubDataModel;
    @Resource
    private OtherExpenseSubDataModel otherExpenseSubDataModel;
    @Resource
    private FundTransferSubDataModel fundTransferSubDataModel;
    @Resource
    private EsHelper esHelper;
    @Resource
    private ClueSubDataModel clueSubDataModel;
    @Resource
    private MarketActivitySubDataModel marketActivitySubDataModel;
    @Resource
    private CreditTemporaryLimitSubDataModel creditTemporaryLimitSubDataModel;
    @Resource
    private CreditFixedLimitSubDataModel creditFixedLimitSubDataModel;
    @Resource
    private CostAdjustSubDataModel costAdjustSubDataModel;
    @Resource
    private IndexTypeModel indexTypeModel;
    @Resource
    private CompetitorSubDataModel competitorSubDataModel;
    @Resource
    private PaasFormPaasSubDataDao paasFormPaasSubDataDao;
    @Resource
    private PaasFormSubDataEsModel paasFormSubDataEsModel;


    @Override
    public void findSubEntity(String corpid, List<? extends PaasFormDataEntity> entitys, IndexTypeEnum indexTypeEnum) {
        if (CollectionUtils.isEmpty(entitys)) {
            return;
        }
        Map<Long, Map<String, JSONArray>> subMap = getSubMap(corpid, entitys, indexTypeEnum);

        entitys.forEach(item -> {
            if (Objects.nonNull(item)) {
                Map<String, JSONArray> subData = subMap.get(item.getId());
                if (Objects.nonNull(subData)) {
                    subData.forEach((k, v) -> {
                        if (Objects.nonNull(item.getData())) {
                            item.getData().put(k, v);
                        }
                    });
                }
            }
        });
    }

    private Map<Long, Map<String, JSONArray>> getSubMap(String corpid, List<? extends PaasFormDataEntity> entitys, IndexTypeEnum indexTypeEnum) {
        if (Objects.isNull(entitys) || entitys.isEmpty() || Objects.isNull(entitys.get(0))) {
            return new HashMap<>();
        }
        List<Long> ids = new ArrayList<>();
        entitys.forEach(v -> ids.add(v.getId()));

        Map<String, Object> map = new HashMap<>();
        map.put("corpid", corpid);
        map.put("dataIdIn", ids);
        map.put("del", DelEnum.NORMAL.getDel());
        List<? extends PaasFormSubDataEntity> subDataEntities = getSubDataList(map, indexTypeEnum);
        Map<Long, Map<String, JSONArray>> subMap = new HashMap<>();
        for (PaasFormSubDataEntity subDataEntity : subDataEntities) {
            JSONObject data = subDataEntity.getData();
            data.put(BusinessConstant.SUB_DATA_ID, subDataEntity.getId());
            subDataEntity.setData(data);
            subMap.computeIfAbsent(subDataEntity.getDataId(), v -> new HashMap<>()).computeIfAbsent(subDataEntity.getAttr(), v -> new JSONArray()).add(data);
        }
        return subMap;
    }

    @Override
    public void findSubEntity4Invoice(List<? extends PaasFormDataEntity> entitys, String corpid) {
        Map<Long, Map<String, JSONArray>> subMap = getSubMap(corpid, entitys, IndexTypeEnum.IDX_SAAS_INVOICE_EXPRESS);

        entitys.forEach(item -> {
            if (Objects.nonNull(item)) {
                JSONObject data = item.getData();
                if (Objects.nonNull(data)) {
                    data.put(InvoiceEnum.CUSTOMER_INVOICE.getAttr(), Collections.singletonList(new JSONObject()));
                    data.put(InvoiceEnum.CUSTOMER_EXPRESS.getAttr(), Collections.singletonList(new JSONObject()));
                }
                Map<String, JSONArray> subData = subMap.get(item.getId());
                if (Objects.nonNull(subData)) {
                    subData.forEach((k, v) -> item.getData().put(k, v));
                }
            }
        });

    }

    @Override
    public void transferSubForm(PaasFormDataEntity entity, IndexTypeEnum indexTypeEnum) {
        JSONObject subData = new JSONObject();
        JSONObject data = entity.getData() == null ? new JSONObject() : entity.getData();
        Iterator<Map.Entry<String, Object>> iterator = data.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> next = iterator.next();
            if (next.getKey().contains(FieldTypeEnum.SUB_FORM.getEsalias())) {
                if (!EsHelper.isSpecialSubForm(next.getKey(), indexTypeEnum) && Objects.nonNull(next.getValue())) {
                    subData.put(next.getKey(), next.getValue());
                    iterator.remove();
                } else {
                    // 客户主联系人字段过滤，数据库和es都不存，只在列表页回显使用
                    boolean isCustomer = Objects.equals(indexTypeEnum, IndexTypeEnum.IDX_SAAS_CUSTOMER) || Objects.equals(indexTypeEnum, IndexTypeEnum.IDX_SAAS_CUSTOMER_SPECIAL);
                    if ( isCustomer && Objects.equals(next.getKey(), CustomerManagementEnum.CONTACT_PHONE.getAttr())) {
                        iterator.remove();
                    }
                }
            }
        }
        entity.setSubFormData(subData);
    }

    /**
     * 孤独子表单
     * @param entity
     * @param indexTypeEnum
     * @author xingxing.xiao
     * @date 2020/10/12 21:52
     */
    @Override
    public void transferSingleSubForm(PaasFormDataEntity entity, IndexTypeEnum indexTypeEnum) {
        JSONObject subData = new JSONObject();
        JSONObject data = entity.getData() == null ? new JSONObject() : entity.getData();
        Iterator<Map.Entry<String, Object>> iterator = data.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> next = iterator.next();
            if (next.getKey().contains(FieldTypeEnum.SUB_FORM.getEsalias())) {
                boolean singleSubForm = false;
                switch (indexTypeEnum) {
                    case IDX_SAAS_INVOICE_EXPRESS:
                        if (next.getKey().equals(InvoiceEnum.CUSTOMER_INVOICE.getAttr()) || next.getKey().equals(InvoiceEnum.CUSTOMER_EXPRESS.getAttr())) {
                            singleSubForm = true;
                        }
                        break;
                    case IDX_SAAS_PURCHASE_INVOICE_EXPRESS:
                        if (next.getKey().equals(PurchaseInvoiceEnum.CUSTOMER_INVOICE.getAttr()) || next.getKey().equals(PurchaseInvoiceEnum.CUSTOMER_EXPRESS.getAttr())) {
                            singleSubForm = true;
                        }
                        break;
                    default:
                        break;
                }
                if (singleSubForm && Objects.nonNull(next.getValue())) {
                    subData.put(next.getKey(), next.getValue());
                    iterator.remove();
                }
            }
        }
        entity.setSingleSubFormData(subData);
    }

    @Override
    public Integer insertSubFormData(PaasFormDataEntity entity, IndexTypeEnum indexTypeEnum) throws XbbException {
        JSONObject subFormData = entity.getSubFormData();
        if (Objects.isNull(subFormData)) {
            return 0;
        }
        List<? extends PaasFormSubDataEntity> subInsertList = formatInsertSubData(entity, subFormData);
        Integer integer = 0;
        if (subInsertList.size() == 0) {
            return integer;
        }
        switch (indexTypeEnum) {
            case IDX_SUB_FORM_DATA:
                integer = paasFormPaasSubDataModel.insertBatch((List<PaasFormSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_CUSTOMER_SUB:
            case IDX_SAAS_CUSTOMER_SUB_SPECIAL:
                integer = customerSubDataModel.insertBatch((List<CustomerSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_CONTRACT_SUB:
                List<ContractSubDataEntity> newsubInsertList = (List<ContractSubDataEntity>) subInsertList;
                integer = contractSubDataModel.insertBatch(newsubInsertList);
                break;
            case IDX_SAAS_QUOTATION_SUB:
                integer = quotationSubDataModel.insertBatch((List<QuotationSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_OUTSTOCK_SUB:
                integer = outstockSubDataModel.insertBatch((List<OutstockSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_CONTACT_SUB:
                integer = contactSubDataModel.insertBatch((List<ContactSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_INVOICE_SUB:
                integer = invoiceSubDataModel.insertBatch((List<InvoiceSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_PURCHASE_INVOICE_SUB:
                integer = purchaseInvoiceSubDataModel.insertBatch((List<PurchaseInvoiceSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_INSTOCK_SUB:
                integer = instockSubDataModel.insertBatch((List<InstockSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_ASSEMBLE_SUB:
                integer = assembleSubDataModel.insertBatch((List<AssembleSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_PAYMENT_SUB:
                integer = paymentSubDataModel.insertBatch((List<PaymentSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_PAYMENT_SHEET_SUB:
                integer = paymentSheetSubDataModel.insertBatch((List<PaymentSheetSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_PAYMENT_TASK_SUB:
                integer = paymentTaskSubDataModel.insertBatch((List<PaymentTaskSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_PAYPLAN_SHEET_SUB:
                integer = payPlanSheetSubDataModel.insertBatch((List<PayPlanSheetSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_PRODUCTION_ORDER_SUB:
                integer = productionOrderSubDataModel.insertBatch((List<ProductionOrderSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_PAY_PLAN_SUB:
                integer = payPlanSubDataModel.insertBatch((List<PayPlanSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_COMMUNICATE_PLAN_SUB:
                integer = communicatePlanSubDataModel.insertBatch((List<CommunicatePlanSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_OPPORTUNITY_SUB:
                integer = opportunitySubDataModel.insertBatch((List<OpportunitySubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_BOM_SUB:
                integer = bomBillSubDataModel.insertBatch((List<BomBillSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_CUSTOMER_COMMUNICATE_SUB:
                integer = customerCommunicateSubDataModel.insertBatch((List<CustomerCommunicateSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_SUPPLIER_COMMUNICATE_SUB:
                integer = supplierCommunicateSubDataModel.insertBatch((List<SupplierCommunicateSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_WORK_REPORT_SUB:
                integer = workReportSubDataModel.insertBatch((List<WorkReportSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_PURCHASE_SUB:
                integer = purchaseSubDataModel.insertBatch((List<PurchaseSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_RETURNED_PURCHASE_SUB:
                integer = returnedPurchaseSubDataModel.insertBatch((List<ReturnedPurchaseSubDataEntity>)subInsertList);
                break;
            case IDX_SAAS_SUPPLIER_CONTACT_SUB:
                integer = supplierContactSubDataModel.insertBatch((List<SupplierContactSubDataEntity>)subInsertList);
                break;
            case IDX_SAAS_SUPPLIER_SUB:
                integer = supplierSubDataModel.insertBatch((List<SupplierSubDataEntity>)subInsertList);
                break;
            case IDX_SAAS_PRODUCT_SUB:
                integer = productSubDataModel.insertBatch((List<ProductSubDataEntity>)subInsertList);
                break;
            case IDX_SAAS_INVENTORY_SUB:
                integer = inventorySubDataModel.insertBatch((List<InventorySubDataEntity>)subInsertList);
                break;
            case IDX_SAAS_TRANSFER_SUB:
                integer = transferSubDataModel.insertBatch((List<TransferSubDataEntity>)subInsertList);
                break;
            case IDX_SAAS_WAREHOUSE_SUB:
                integer = warehouseSubDataModel.insertBatch((List<WarehouseSubDataEntity>)subInsertList);
                break;
            case IDX_SAAS_REFUND_SUB:
                integer = refundSubDataModel.insertBatch((List<RefundSubDataEntity>)subInsertList);
                break;
            case IDX_SAAS_WORK_ORDER_SUB:
                integer = workOrderSubDataModel.insertBatch((List<WorkOrderSubDataEntity>)subInsertList);
                break;
            case IDX_SAAS_CLUE_SUB:
                integer = clueSubDataModel.insertBatch((List<ClueSubDataEntity>)subInsertList);
                break;
            case IDX_SAAS_MARKET_ACTIVITY_SUB:
                integer = marketActivitySubDataModel.insertBatch((List<MarketActivitySubDataEntity>)subInsertList);
                break;
            case IDX_SAAS_CREDIT_TEMPORARY_LIMIT_SUB:
                integer = creditTemporaryLimitSubDataModel.insertBatch((List<CreditTemporaryLimitSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_CREDIT_FIXED_LIMIT_SUB:
                integer = creditFixedLimitSubDataModel.insertBatch((List<CreditFixedLimitSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_OTHER_INCOME_SUB:
                integer = otherIncomeSubDataModel.insertBatch((List<OtherIncomeSubDataEntity>)subInsertList);
                break;
            case IDX_SAAS_OTHER_EXPENSE_SUB:
                integer = otherExpenseSubDataModel.insertBatch((List<OtherExpenseSubDataEntity>)subInsertList);
                break;
            case IDX_SAAS_FUND_TRANSFER_SUB:
                integer = fundTransferSubDataModel.insertBatch((List<FundTransferSubDataEntity>)subInsertList);
                break;
            case IDX_SAAS_COST_ADJUST_SUB:
                integer = costAdjustSubDataModel.insertBatch((List<CostAdjustSubDataEntity>)subInsertList);
                break;
            case IDX_SAAS_COMPETITOR_SUB:
                integer = competitorSubDataModel.insertBatch((List<CompetitorSubDataEntity>)subInsertList);
                break;
            case IDX_SERVICE_PROJECT_SUB:
                integer = paasFormPaasSubDataDao.insertBatch(subInsertList,SubFormTableNameConstant.TB_WORK_ORDER_V2_SERVICE_PROJECT_SUB_DATA);
                break;
            case IDX_RECEIPT_ORDER_SUB:
                integer = paasFormPaasSubDataDao.insertBatch(subInsertList,SubFormTableNameConstant.TB_WORK_ORDER_V2_RECEIPT_SUB_DATA);
                break;
            case IDX_WORK_ORDER_V2_SUB:
                integer = paasFormPaasSubDataDao.insertBatch(subInsertList,SubFormTableNameConstant.TB_WORK_ORDER_V2_SUB_DATA);
                break;
            default:
                break;
        }
        if (CollectionsUtil.isNotEmpty(subInsertList)) {
            JSONObject subFormData1 = new JSONObject();
            subInsertList.forEach(item->{
                if (subFormData1.containsKey(item.getAttr())) {
                    JSONArray subFormList = subFormData1.getJSONArray(item.getAttr());
                    item.getData().put(BusinessConstant.SUB_DATA_ID, item.getId());
                    subFormList.add(item.getData());
                    subFormData1.put(item.getAttr(), subFormList);
                } else {
                    JSONArray subFormList = new JSONArray();
                    item.getData().put(BusinessConstant.SUB_DATA_ID, item.getId());
                    subFormList.add(item.getData());
                    subFormData1.put(item.getAttr(), subFormList);
                }
            });
            entity.setSubFormData(subFormData1);
            entity.getData().putAll(subFormData1);
        }
        return integer;
    }

    /**
     * 孤独子表单
     * @param entity
     * @param indexTypeEnum
     * @author xingxing.xiao
     * @date 2020/10/12 21:52
     */
    @Override
    public Integer insertSingleSubFormData(PaasFormDataEntity entity, IndexTypeEnum indexTypeEnum) throws XbbException {
        JSONObject subFormData = entity.getSingleSubFormData();
        if (Objects.isNull(subFormData)) {
            return 0;
        }
        List<? extends PaasFormSubDataEntity> subInsertList = formatInsertSubData(entity, subFormData);
        Integer integer = 0;
        if (subInsertList.size() == 0) {
            return integer;
        }
        switch (indexTypeEnum) {
            case IDX_SAAS_INVOICE_EXPRESS:
                integer = invoiceExpressSubDataModel.insertBatch((List<InvoiceExpressSubDataEntity>) subInsertList);
                break;
            case IDX_SAAS_PURCHASE_INVOICE_EXPRESS:
                integer = purchaseInvoiceExpressSubDataModel.insertBatch((List<PurchaseInvoiceExpressSubDataEntity>) subInsertList);
                break;
            default:
                break;
        }
        return integer;
    }

    private List<? extends PaasFormSubDataEntity> formatInsertSubData(PaasFormDataEntity entity, JSONObject subFormData) {
        List<PaasFormSubDataEntity> subInsertList = new ArrayList<>();

        subFormData.forEach((key, value1) -> {
            JSONArray value = JSON.parseArray(JSON.toJSONString(value1));
            value.forEach(v -> {
                JSONObject jsonObject = (JSONObject) v;
                jsonObject.remove(BusinessConstant.SUB_DATA_ID);
                subInsertList.add(new PaasFormSubDataEntity(entity.getFormId(), entity.getCorpid(), entity.getId(), key, jsonObject));
            });
        });
        return subInsertList;
    }


    @Override
    public Integer insertBatchSubFormData(List<? extends PaasFormDataEntity> entities, IndexTypeEnum indexTypeEnum) throws XbbException {
        Integer integer = 0;
        if (Objects.isNull(entities) || entities.isEmpty()) {
            return integer;
        }
        String corpid = entities.get(0).getCorpid();
        List<? extends PaasFormSubDataEntity> insertSubList = formatInsertBatchSubData(entities, corpid);
        if (insertSubList.isEmpty()) {
            return integer;
        }
        switch (indexTypeEnum) {
            case IDX_SUB_FORM_DATA:
                integer = paasFormPaasSubDataModel.insertBatch((List<PaasFormSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_CUSTOMER_SUB:
            case IDX_SAAS_CUSTOMER_SUB_SPECIAL:
                integer = customerSubDataModel.insertBatch((List<CustomerSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_CONTRACT_SUB:
                integer = contractSubDataModel.insertBatch((List<ContractSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_QUOTATION_SUB:
                integer = quotationSubDataModel.insertBatch((List<QuotationSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_OUTSTOCK_SUB:
                integer = outstockSubDataModel.insertBatch((List<OutstockSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_CONTACT_SUB:
                integer = contactSubDataModel.insertBatch((List<ContactSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_INVOICE_SUB:
                integer = invoiceSubDataModel.insertBatch((List<InvoiceSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_PURCHASE_INVOICE_SUB:
                integer = purchaseInvoiceSubDataModel.insertBatch((List<PurchaseInvoiceSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_INSTOCK_SUB:
                integer = instockSubDataModel.insertBatch((List<InstockSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_ASSEMBLE_SUB:
                integer = assembleSubDataModel.insertBatch((List<AssembleSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_PAYMENT_SUB:
                integer = paymentSubDataModel.insertBatch((List<PaymentSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_PAYMENT_SHEET_SUB:
                integer = paymentSheetSubDataModel.insertBatch((List<PaymentSheetSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_PAYPLAN_SHEET_SUB:
                integer = payPlanSheetSubDataModel.insertBatch((List<PayPlanSheetSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_PRODUCTION_ORDER_SUB:
                integer = productionOrderSubDataModel.insertBatch((List<ProductionOrderSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_PAY_PLAN_SUB:
                integer = payPlanSubDataModel.insertBatch((List<PayPlanSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_COMMUNICATE_PLAN_SUB:
                integer = communicatePlanSubDataModel.insertBatch((List<CommunicatePlanSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_OPPORTUNITY_SUB:
                integer = opportunitySubDataModel.insertBatch((List<OpportunitySubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_BOM_SUB:
                integer = bomBillSubDataModel.insertBatch((List<BomBillSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_CUSTOMER_COMMUNICATE_SUB:
                integer = customerCommunicateSubDataModel.insertBatch((List<CustomerCommunicateSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_SUPPLIER_COMMUNICATE_SUB:
                integer = supplierCommunicateSubDataModel.insertBatch((List<SupplierCommunicateSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_WORK_REPORT_SUB:
                integer = workReportSubDataModel.insertBatch((List<WorkReportSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_PURCHASE_SUB:
                integer = purchaseSubDataModel.insertBatch((List<PurchaseSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_RETURNED_PURCHASE_SUB:
                integer = returnedPurchaseSubDataModel.insertBatch((List<ReturnedPurchaseSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_SUPPLIER_CONTACT_SUB:
                integer = supplierContactSubDataModel.insertBatch((List<SupplierContactSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_SUPPLIER_SUB:
                integer = supplierSubDataModel.insertBatch((List<SupplierSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_PRODUCT_SUB:
                integer = productSubDataModel.insertBatch((List<ProductSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_INVENTORY_SUB:
                integer = inventorySubDataModel.insertBatch((List<InventorySubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_TRANSFER_SUB:
                integer = transferSubDataModel.insertBatch((List<TransferSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_WAREHOUSE_SUB:
                integer = warehouseSubDataModel.insertBatch((List<WarehouseSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_REFUND_SUB:
                integer = refundSubDataModel.insertBatch((List<RefundSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_WORK_ORDER_SUB:
                integer = workOrderSubDataModel.insertBatch((List<WorkOrderSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_CLUE_SUB:
                integer = clueSubDataModel.insertBatch((List<ClueSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_MARKET_ACTIVITY_SUB:
                integer = marketActivitySubDataModel.insertBatch((List<MarketActivitySubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_CREDIT_TEMPORARY_LIMIT_SUB:
                integer = creditTemporaryLimitSubDataModel.insertBatch((List<CreditTemporaryLimitSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_CREDIT_FIXED_LIMIT_SUB:
                integer = creditFixedLimitSubDataModel.insertBatch((List<CreditFixedLimitSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_OTHER_INCOME_SUB:
                integer = otherIncomeSubDataModel.insertBatch((List<OtherIncomeSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_OTHER_EXPENSE_SUB:
                integer = otherExpenseSubDataModel.insertBatch((List<OtherExpenseSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_FUND_TRANSFER_SUB:
                integer = fundTransferSubDataModel.insertBatch((List<FundTransferSubDataEntity>) insertSubList);
                break;
            case IDX_SERVICE_PROJECT_SUB:
                integer = paasFormPaasSubDataDao.insertBatch(insertSubList, SubFormTableNameConstant.TB_WORK_ORDER_V2_SERVICE_PROJECT_SUB_DATA);
                break;
            default:
                break;
        }
        return integer;
    }

    private List<PaasFormSubDataEntity> formatInsertBatchSubData(List<? extends PaasFormDataEntity> entities, String corpid) {
        List<PaasFormSubDataEntity> insertSubList = new ArrayList<>();
        for (PaasFormDataEntity entity : entities) {
            JSONObject subFormData = entity.getSubFormData();
            if (Objects.isNull(subFormData)) {
                continue;
            }
            Set<Map.Entry<String, Object>> subData = subFormData.entrySet();
            for (Map.Entry<String, Object> entry : subData) {
                JSONArray value = JSON.parseArray(JSON.toJSONString(entry.getValue())) ;
                for (Object o : value) {
                    JSONObject object = (JSONObject) o;
                    object.remove(BusinessConstant.SUB_DATA_ID);
                    PaasFormSubDataEntity paasFormSubDataEntity = new PaasFormSubDataEntity(entity.getFormId(), corpid, entity.getId(), entry.getKey(), object);
                    insertSubList.add(paasFormSubDataEntity);
                }

            }
        }
        return insertSubList;
    }

    @Override
    public Integer insertBatchSingleSubFormData(List<? extends PaasFormDataEntity> entities, IndexTypeEnum indexTypeEnum) throws XbbException {
        Integer integer = 0;
        if (Objects.isNull(entities) || entities.isEmpty()) {
            return integer;
        }
        String corpid = entities.get(0).getCorpid();
        List<? extends PaasFormSubDataEntity> insertSubList = formatInsertBatchSingleSubData(entities, corpid);
        if (insertSubList.isEmpty()) {
            return integer;
        }
        switch (indexTypeEnum) {
            case IDX_SAAS_INVOICE_EXPRESS:
                integer = invoiceExpressSubDataModel.insertBatch((List<InvoiceExpressSubDataEntity>) insertSubList);
                break;
            case IDX_SAAS_PURCHASE_INVOICE_EXPRESS:
                integer = purchaseInvoiceExpressSubDataModel.insertBatch((List<PurchaseInvoiceExpressSubDataEntity>) insertSubList);
                break;
            default:
                break;
        }
        return integer;
    }

    private List<PaasFormSubDataEntity> formatInsertBatchSingleSubData(List<? extends PaasFormDataEntity> entities, String corpid) {
        List<PaasFormSubDataEntity> insertSubList = new ArrayList<>();
        for (PaasFormDataEntity entity : entities) {
            JSONObject subFormData = entity.getSingleSubFormData();
            if (Objects.isNull(subFormData)) {
                continue;
            }
            Set<Map.Entry<String, Object>> subData = subFormData.entrySet();
            for (Map.Entry<String, Object> entry : subData) {
                JSONArray value = (JSONArray) entry.getValue();
                for (Object o : value) {
                    JSONObject object = (JSONObject) o;
                    if (Objects.nonNull(object) && !object.isEmpty()) {
                        PaasFormSubDataEntity paasFormSubDataEntity = new PaasFormSubDataEntity(entity.getFormId(), corpid, entity.getId(), entry.getKey(), object);
                        insertSubList.add(paasFormSubDataEntity);
                    }
                }

            }
        }
        return insertSubList;
    }

    @Override
    public Integer updateSubFormData(PaasFormDataEntity paasFormDataEntity, IndexTypeEnum indexTypeEnum) throws XbbException {
        // 传过来的data是null，是不需要更新data内的数据的，也不需要更新子表单数据，直接返回
        if (Objects.isNull(paasFormDataEntity.getData())) {
            return 0;
        }
        String corpid = paasFormDataEntity.getCorpid();
        Long dataId = paasFormDataEntity.getId();

        List<? extends PaasFormSubDataEntity> paasFormPaasSubDataEntities = new ArrayList<>();

        switch (indexTypeEnum) {
            case IDX_SUB_FORM_DATA:
                paasFormPaasSubDataEntities = paasFormPaasSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_CUSTOMER_SUB:
            case IDX_SAAS_CUSTOMER_SUB_SPECIAL:
                paasFormPaasSubDataEntities = customerSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_CONTRACT_SUB:
                paasFormPaasSubDataEntities = contractSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_QUOTATION_SUB:
                paasFormPaasSubDataEntities = quotationSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_OUTSTOCK_SUB:
                paasFormPaasSubDataEntities = outstockSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_CONTACT_SUB:
                paasFormPaasSubDataEntities = contactSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_INVOICE_SUB:
                paasFormPaasSubDataEntities = invoiceSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_INVOICE_EXPRESS:
                paasFormPaasSubDataEntities = invoiceExpressSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_PURCHASE_INVOICE_SUB:
                paasFormPaasSubDataEntities = purchaseInvoiceSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_PURCHASE_INVOICE_EXPRESS:
                paasFormPaasSubDataEntities = purchaseInvoiceExpressSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_INSTOCK_SUB:
                paasFormPaasSubDataEntities = instockSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_ASSEMBLE_SUB:
                paasFormPaasSubDataEntities = assembleSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_PAYMENT_SUB:
                paasFormPaasSubDataEntities = paymentSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_PAYMENT_SHEET_SUB:
                paasFormPaasSubDataEntities = paymentSheetSubDataModel.getByDataId(paasFormDataEntity.getId(), corpid);
                break;
            case IDX_SAAS_PAYMENT_TASK_SUB:
                paasFormPaasSubDataEntities = paymentTaskSubDataModel.getByDataId(paasFormDataEntity.getId(), corpid);
                break;
            case IDX_SAAS_PAYPLAN_SHEET_SUB:
                paasFormPaasSubDataEntities = payPlanSheetSubDataModel.getByDataId(paasFormDataEntity.getId(), corpid);
                break;
            case IDX_SAAS_PRODUCTION_ORDER_SUB:
                paasFormPaasSubDataEntities = productionOrderSubDataModel.getByDataId(paasFormDataEntity.getId(), corpid);
                break;
            case IDX_SAAS_PAY_PLAN_SUB:
                paasFormPaasSubDataEntities = payPlanSubDataModel.getByDataId(paasFormDataEntity.getId(), corpid);
                break;
            case IDX_SAAS_COMMUNICATE_PLAN_SUB:
                paasFormPaasSubDataEntities = communicatePlanSubDataModel.getByDataId(paasFormDataEntity.getId(), corpid);
                break;
            case IDX_SAAS_OPPORTUNITY_SUB:
                paasFormPaasSubDataEntities = opportunitySubDataModel.getByDataId(paasFormDataEntity.getId(), corpid);
                break;
            case IDX_SAAS_BOM_SUB:
                paasFormPaasSubDataEntities = bomBillSubDataModel.getByDataId(paasFormDataEntity.getId(), corpid);
                break;
            case IDX_SAAS_CUSTOMER_COMMUNICATE_SUB:
                paasFormPaasSubDataEntities = customerCommunicateSubDataModel.getByDataId(paasFormDataEntity.getId(), corpid);
                break;
            case IDX_SAAS_SUPPLIER_COMMUNICATE_SUB:
                paasFormPaasSubDataEntities = supplierCommunicateSubDataModel.getByDataId(paasFormDataEntity.getId(), corpid);
                break;
            case IDX_SAAS_WORK_REPORT_SUB:
                paasFormPaasSubDataEntities = workReportSubDataModel.getByDataId(paasFormDataEntity.getId(), corpid);
                break;
            case IDX_SAAS_PURCHASE_SUB:
                paasFormPaasSubDataEntities = purchaseSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_RETURNED_PURCHASE_SUB:
                paasFormPaasSubDataEntities = returnedPurchaseSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_SUPPLIER_CONTACT_SUB:
                paasFormPaasSubDataEntities = supplierContactSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_SUPPLIER_SUB:
                paasFormPaasSubDataEntities = supplierSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_PRODUCT_SUB:
                paasFormPaasSubDataEntities = productSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_INVENTORY_SUB:
                paasFormPaasSubDataEntities = inventorySubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_TRANSFER_SUB:
                paasFormPaasSubDataEntities = transferSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_WAREHOUSE_SUB:
                paasFormPaasSubDataEntities = warehouseSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_REFUND_SUB:
                paasFormPaasSubDataEntities = refundSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_WORK_ORDER_SUB:
                paasFormPaasSubDataEntities = workOrderSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_CLUE_SUB:
                paasFormPaasSubDataEntities = clueSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_MARKET_ACTIVITY_SUB:
                paasFormPaasSubDataEntities = marketActivitySubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_CREDIT_TEMPORARY_LIMIT_SUB:
                paasFormPaasSubDataEntities = creditTemporaryLimitSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_CREDIT_FIXED_LIMIT_SUB:
                paasFormPaasSubDataEntities = creditFixedLimitSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_OTHER_INCOME_SUB:
                paasFormPaasSubDataEntities = otherIncomeSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_OTHER_EXPENSE_SUB:
                paasFormPaasSubDataEntities = otherExpenseSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_FUND_TRANSFER_SUB:
                paasFormPaasSubDataEntities = fundTransferSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_COST_ADJUST_SUB:
                paasFormPaasSubDataEntities = costAdjustSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SAAS_COMPETITOR_SUB:
                paasFormPaasSubDataEntities =  competitorSubDataModel.getByDataId(dataId, corpid);
                break;
            case IDX_SERVICE_PROJECT_SUB:
                paasFormPaasSubDataEntities = paasFormPaasSubDataDao.getByDataId(dataId, corpid, SubFormTableNameConstant.TB_WORK_ORDER_V2_SERVICE_PROJECT_SUB_DATA);
                break;
            case IDX_RECEIPT_ORDER_SUB:
                paasFormPaasSubDataEntities = paasFormPaasSubDataDao.getByDataId(dataId, corpid, SubFormTableNameConstant.TB_WORK_ORDER_V2_RECEIPT_SUB_DATA);
                break;
            case IDX_WORK_ORDER_V2_SUB:
                paasFormPaasSubDataEntities = paasFormPaasSubDataDao.getByDataId(dataId, corpid, SubFormTableNameConstant.TB_WORK_ORDER_V2_SUB_DATA);
                break;
            default:
                break;
        }

        JSONObject subFormData;
        if (Objects.equals(indexTypeEnum, IndexTypeEnum.IDX_SAAS_INVOICE_EXPRESS) || Objects.equals(indexTypeEnum, IndexTypeEnum.IDX_SAAS_PURCHASE_INVOICE_EXPRESS)) {
            subFormData = paasFormDataEntity.getSingleSubFormData();
        } else {
            subFormData = paasFormDataEntity.getSubFormData();
        }
        if (subFormData == null) {
            return 0;
        }
        UpdateFormatSubDataDTO updateFormatSubDataDTO = formatUpdateSubData(paasFormDataEntity, paasFormPaasSubDataEntities, subFormData);
        List<UpdateDataEntity> updateList = updateFormatSubDataDTO.getUpdateList();
        List<? extends PaasFormSubDataEntity> insertList = updateFormatSubDataDTO.getInsertList();
        List<? extends PaasFormSubDataEntity> deleteList = updateFormatSubDataDTO.getDeleteList();
        List<? extends PaasFormSubDataEntity> updateDelList = updateFormatSubDataDTO.getUpdateDelList();
        Integer delete = 0;
        Integer update = 0;
        Integer insert = 0;
        switch (indexTypeEnum) {
            case IDX_SUB_FORM_DATA:
                if (!deleteList.isEmpty()) {
                   delete = paasFormPaasSubDataModel.deleteBatch(corpid, (List<PaasFormSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                   insert = paasFormPaasSubDataModel.insertBatch((List<PaasFormSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                   update = paasFormPaasSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = paasFormPaasSubDataModel.updateDel((List<PaasFormSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_CUSTOMER_SUB:
            case IDX_SAAS_CUSTOMER_SUB_SPECIAL:
                if (!deleteList.isEmpty()) {
                   delete = customerSubDataModel.deleteBatch(corpid, (List<CustomerSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                   insert = customerSubDataModel.insertBatch((List<CustomerSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                   update = customerSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = customerSubDataModel.updateDel((List<CustomerSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_CONTRACT_SUB:
                if (!deleteList.isEmpty()) {
                   delete = contractSubDataModel.deleteBatch(corpid, (List<ContractSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                   insert = contractSubDataModel.insertBatch((List<ContractSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                   update = contractSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = contractSubDataModel.updateDel((List<ContractSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_QUOTATION_SUB:
                if (!deleteList.isEmpty()) {
                   delete = quotationSubDataModel.deleteBatch(corpid, (List<QuotationSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                   insert = quotationSubDataModel.insertBatch((List<QuotationSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = quotationSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = quotationSubDataModel.updateDel((List<QuotationSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_OUTSTOCK_SUB:
                if (!deleteList.isEmpty()) {
                    delete = outstockSubDataModel.deleteBatch(corpid, (List<OutstockSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = outstockSubDataModel.insertBatch((List<OutstockSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = outstockSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = outstockSubDataModel.updateDel((List<OutstockSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_CONTACT_SUB:
                if (!deleteList.isEmpty()) {
                   delete = contactSubDataModel.deleteBatch(corpid, (List<ContactSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                   insert = contactSubDataModel.insertBatch((List<ContactSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = contactSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = contactSubDataModel.updateDel((List<ContactSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_INVOICE_SUB:
                if (!deleteList.isEmpty()) {
                    delete = invoiceSubDataModel.deleteBatch(corpid, (List<InvoiceSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                   insert = invoiceSubDataModel.insertBatch((List<InvoiceSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = invoiceSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = invoiceSubDataModel.updateDel((List<InvoiceSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_INVOICE_EXPRESS:
                if (!deleteList.isEmpty()) {
                    delete = invoiceExpressSubDataModel.deleteBatch(corpid, (List<InvoiceExpressSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = invoiceExpressSubDataModel.insertBatch((List<InvoiceExpressSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = invoiceExpressSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = invoiceExpressSubDataModel.updateDel((List<InvoiceExpressSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_PURCHASE_INVOICE_SUB:
                if (!deleteList.isEmpty()) {
                    delete =  purchaseInvoiceSubDataModel.deleteBatch(corpid, (List<PurchaseInvoiceSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                   insert = purchaseInvoiceSubDataModel.insertBatch((List<PurchaseInvoiceSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                   update = purchaseInvoiceSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = purchaseInvoiceSubDataModel.updateDel((List<PurchaseInvoiceSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_PURCHASE_INVOICE_EXPRESS:
                if (!deleteList.isEmpty()) {
                    delete =  purchaseInvoiceExpressSubDataModel.deleteBatch(corpid, (List<PurchaseInvoiceExpressSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = purchaseInvoiceExpressSubDataModel.insertBatch((List<PurchaseInvoiceExpressSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = purchaseInvoiceExpressSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = purchaseInvoiceExpressSubDataModel.updateDel((List<PurchaseInvoiceExpressSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_INSTOCK_SUB:
                if (!deleteList.isEmpty()) {
                    delete = instockSubDataModel.deleteBatch(corpid, (List<InstockSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                   insert = instockSubDataModel.insertBatch((List<InstockSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = instockSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = instockSubDataModel.updateDel((List<InstockSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_ASSEMBLE_SUB:
                if (!deleteList.isEmpty()) {
                    delete = assembleSubDataModel.deleteBatch(corpid, (List<AssembleSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = assembleSubDataModel.insertBatch((List<AssembleSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = assembleSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = assembleSubDataModel.updateDel((List<AssembleSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_PAYMENT_SUB:
                if (!deleteList.isEmpty()) {
                   delete = paymentSubDataModel.deleteBatch(corpid, (List<PaymentSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                   insert = paymentSubDataModel.insertBatch((List<PaymentSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = paymentSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = paymentSubDataModel.updateDel((List<PaymentSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_PAYMENT_SHEET_SUB:
                if (!deleteList.isEmpty()) {
                    delete = paymentSheetSubDataModel.deleteBatch(corpid, (List<PaymentSheetSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = paymentSheetSubDataModel.insertBatch((List<PaymentSheetSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = paymentSheetSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = paymentSheetSubDataModel.updateDel((List<PaymentSheetSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_PAYMENT_TASK_SUB:
                if (!deleteList.isEmpty()) {
                    delete = paymentTaskSubDataModel.deleteBatch(corpid, (List<PaymentTaskSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = paymentTaskSubDataModel.insertBatch((List<PaymentTaskSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = paymentTaskSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = paymentTaskSubDataModel.updateDel((List<PaymentTaskSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_PAYPLAN_SHEET_SUB:
                if (!deleteList.isEmpty()) {
                    delete = payPlanSheetSubDataModel.deleteBatch(corpid, (List<PayPlanSheetSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = payPlanSheetSubDataModel.insertBatch((List<PayPlanSheetSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = payPlanSheetSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = payPlanSheetSubDataModel.updateDel((List<PayPlanSheetSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_PRODUCTION_ORDER_SUB:
                if (!deleteList.isEmpty()) {
                   delete = productionOrderSubDataModel.deleteBatch(corpid, (List<ProductionOrderSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = productionOrderSubDataModel.insertBatch((List<ProductionOrderSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = productionOrderSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = productionOrderSubDataModel.updateDel((List<ProductionOrderSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_PAY_PLAN_SUB:
                if (!deleteList.isEmpty()) {
                    delete = payPlanSubDataModel.deleteBatch(corpid, (List<PayPlanSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = payPlanSubDataModel.insertBatch((List<PayPlanSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = payPlanSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = payPlanSubDataModel.updateDel((List<PayPlanSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_COMMUNICATE_PLAN_SUB:
                if (!deleteList.isEmpty()) {
                    delete = communicatePlanSubDataModel.deleteBatch(corpid, (List<CommunicatePlanSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = communicatePlanSubDataModel.insertBatch((List<CommunicatePlanSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = communicatePlanSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = communicatePlanSubDataModel.updateDel((List<CommunicatePlanSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_OPPORTUNITY_SUB:
                if (!deleteList.isEmpty()) {
                    delete = opportunitySubDataModel.deleteBatch(corpid, (List<OpportunitySubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = opportunitySubDataModel.insertBatch((List<OpportunitySubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = opportunitySubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = opportunitySubDataModel.updateDel((List<OpportunitySubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_BOM_SUB:
                if (!deleteList.isEmpty()) {
                   delete = bomBillSubDataModel.deleteBatch(corpid, (List<BomBillSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = bomBillSubDataModel.insertBatch((List<BomBillSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = bomBillSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = bomBillSubDataModel.updateDel((List<BomBillSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_CUSTOMER_COMMUNICATE_SUB:
                if (!deleteList.isEmpty()) {
                   delete = customerCommunicateSubDataModel.deleteBatch(corpid, (List<CustomerCommunicateSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = customerCommunicateSubDataModel.insertBatch((List<CustomerCommunicateSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = customerCommunicateSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = customerCommunicateSubDataModel.updateDel((List<CustomerCommunicateSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_SUPPLIER_COMMUNICATE_SUB:
                if (!deleteList.isEmpty()) {
                    delete = supplierCommunicateSubDataModel.deleteBatch(corpid, (List<SupplierCommunicateSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = supplierCommunicateSubDataModel.insertBatch((List<SupplierCommunicateSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = supplierCommunicateSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = supplierCommunicateSubDataModel.updateDel((List<SupplierCommunicateSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_WORK_REPORT_SUB:
                if (!deleteList.isEmpty()) {
                    delete = workReportSubDataModel.deleteBatch(corpid, (List<WorkReportSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = workReportSubDataModel.insertBatch((List<WorkReportSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = workReportSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = workReportSubDataModel.updateDel((List<WorkReportSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_PURCHASE_SUB:
                if (!deleteList.isEmpty()) {
                   delete = purchaseSubDataModel.deleteBatch(corpid, (List<PurchaseSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = purchaseSubDataModel.insertBatch((List<PurchaseSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = purchaseSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = purchaseSubDataModel.updateDel((List<PurchaseSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_RETURNED_PURCHASE_SUB:
                if (!deleteList.isEmpty()) {
                    delete = returnedPurchaseSubDataModel.deleteBatch(corpid, (List<ReturnedPurchaseSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = returnedPurchaseSubDataModel.insertBatch((List<ReturnedPurchaseSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = returnedPurchaseSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = returnedPurchaseSubDataModel.updateDel((List<ReturnedPurchaseSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_SUPPLIER_CONTACT_SUB:
                if (!deleteList.isEmpty()) {
                    delete = supplierContactSubDataModel.deleteBatch(corpid, (List<SupplierContactSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert =  supplierContactSubDataModel.insertBatch((List<SupplierContactSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = supplierContactSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = supplierContactSubDataModel.updateDel((List<SupplierContactSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_SUPPLIER_SUB:
                if (!deleteList.isEmpty()) {
                    delete = supplierSubDataModel.deleteBatch(corpid, (List<SupplierSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = supplierSubDataModel.insertBatch((List<SupplierSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = supplierSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = supplierSubDataModel.updateDel((List<SupplierSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_PRODUCT_SUB:
                if (!deleteList.isEmpty()) {
                    delete = productSubDataModel.deleteBatch(corpid, (List<ProductSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert =  productSubDataModel.insertBatch((List<ProductSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                   update = productSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = productSubDataModel.updateDel((List<ProductSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_INVENTORY_SUB:
                if (!deleteList.isEmpty()) {
                    delete = inventorySubDataModel.deleteBatch(corpid, (List<InventorySubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = inventorySubDataModel.insertBatch((List<InventorySubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = inventorySubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = inventorySubDataModel.updateDel((List<InventorySubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_TRANSFER_SUB:
                if (!deleteList.isEmpty()) {
                    delete = transferSubDataModel.deleteBatch(corpid, (List<TransferSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                   insert = transferSubDataModel.insertBatch((List<TransferSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = transferSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = transferSubDataModel.updateDel((List<TransferSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_WAREHOUSE_SUB:
                if (!deleteList.isEmpty()) {
                    delete =  warehouseSubDataModel.deleteBatch(corpid, (List<WarehouseSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                   insert = warehouseSubDataModel.insertBatch((List<WarehouseSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = warehouseSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = warehouseSubDataModel.updateDel((List<WarehouseSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_REFUND_SUB:
                if (!deleteList.isEmpty()) {
                    delete = refundSubDataModel.deleteBatch(corpid, (List<RefundSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                   insert = refundSubDataModel.insertBatch((List<RefundSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = refundSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = refundSubDataModel.updateDel((List<RefundSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_WORK_ORDER_SUB:
                if (!deleteList.isEmpty()) {
                    delete =  workOrderSubDataModel.deleteBatch(corpid, (List<WorkOrderSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = workOrderSubDataModel.insertBatch((List<WorkOrderSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                   update = workOrderSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = workOrderSubDataModel.updateDel((List<WorkOrderSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_CLUE_SUB:
                if (!deleteList.isEmpty()) {
                    delete =  clueSubDataModel.deleteBatch(corpid, (List<ClueSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = clueSubDataModel.insertBatch((List<ClueSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = clueSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = clueSubDataModel.updateDel((List<ClueSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_MARKET_ACTIVITY_SUB:
                if (!deleteList.isEmpty()) {
                    delete =  marketActivitySubDataModel.deleteBatch(corpid, (List<MarketActivitySubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = marketActivitySubDataModel.insertBatch((List<MarketActivitySubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = marketActivitySubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = marketActivitySubDataModel.updateDel((List<MarketActivitySubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_CREDIT_TEMPORARY_LIMIT_SUB:
                if (!deleteList.isEmpty()) {
                    delete = creditTemporaryLimitSubDataModel.deleteBatch(corpid, (List<CreditTemporaryLimitSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = creditTemporaryLimitSubDataModel.insertBatch((List<CreditTemporaryLimitSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = creditTemporaryLimitSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = creditTemporaryLimitSubDataModel.updateDel((List<CreditTemporaryLimitSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_CREDIT_FIXED_LIMIT_SUB:
                if (!deleteList.isEmpty()) {
                    delete = creditFixedLimitSubDataModel.deleteBatch(corpid, (List<CreditFixedLimitSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = creditFixedLimitSubDataModel.insertBatch((List<CreditFixedLimitSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = creditFixedLimitSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = creditFixedLimitSubDataModel.updateDel((List<CreditFixedLimitSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_OTHER_INCOME_SUB:
                if (!deleteList.isEmpty()) {
                    delete =  otherIncomeSubDataModel.deleteBatch(corpid, (List<OtherIncomeSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = otherIncomeSubDataModel.insertBatch((List<OtherIncomeSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = otherIncomeSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = otherIncomeSubDataModel.updateDel((List<OtherIncomeSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_OTHER_EXPENSE_SUB:
                if (!deleteList.isEmpty()) {
                    delete =  otherExpenseSubDataModel.deleteBatch(corpid, (List<OtherExpenseSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = otherExpenseSubDataModel.insertBatch((List<OtherExpenseSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = otherExpenseSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = otherExpenseSubDataModel.updateDel((List<OtherExpenseSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_FUND_TRANSFER_SUB:
                if (!deleteList.isEmpty()) {
                    delete =  fundTransferSubDataModel.deleteBatch(corpid, (List<FundTransferSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = fundTransferSubDataModel.insertBatch((List<FundTransferSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = fundTransferSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = fundTransferSubDataModel.updateDel((List<FundTransferSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_COST_ADJUST_SUB:
                if (!deleteList.isEmpty()) {
                    delete =  costAdjustSubDataModel.deleteBatch(corpid, (List<CostAdjustSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = costAdjustSubDataModel.insertBatch((List<CostAdjustSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = costAdjustSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = costAdjustSubDataModel.updateDel((List<CostAdjustSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SAAS_COMPETITOR_SUB:
                if (!deleteList.isEmpty()) {
                    delete =  competitorSubDataModel.deleteBatch(corpid, (List<CompetitorSubDataEntity>) deleteList, WriteRequest.RefreshPolicy.NONE);
                }

                if (!insertList.isEmpty()) {
                    insert = competitorSubDataModel.insertBatch((List<CompetitorSubDataEntity>) insertList);
                }

                if (!updateList.isEmpty()) {
                    update = competitorSubDataModel.updateBatch(updateList, corpid);
                }
                if (!updateDelList.isEmpty()) {
                    update = competitorSubDataModel.updateDel((List<CostAdjustSubDataEntity>) updateDelList, corpid, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
                }
                break;
            case IDX_SERVICE_PROJECT_SUB:
            case IDX_RECEIPT_ORDER_SUB:
            case IDX_WORK_ORDER_V2_SUB:
                paasFormPaasSubDataModel.updateSubFormData4WorkOrderV2(deleteList, insertList, updateList, updateDelList, corpid, indexTypeEnum);
                break;
            default:
                break;
        }
        //因为目前工作流审批这边需要通过子表单的dataId去进行操作，所以新增的子表单也是需要有dataId，所以对数据需要重新封装一下
        packageNewSubFormData(insertList, paasFormDataEntity, indexTypeEnum);
        if (delete != 0 || insert != 0 || update != 0) {
            return 1;
        }
        return 0;
    }

    /**
     * 因为入参的子表单的新增数据是没有dataId，所以这边需要重新封装一下
     *
     * @param insertList
     * @param paasFormDataEntity
     * @param indexTypeEnum
     */
    private void packageNewSubFormData (List<? extends PaasFormSubDataEntity> insertList, PaasFormDataEntity paasFormDataEntity, IndexTypeEnum indexTypeEnum) {
        JSONObject subFormData;
        if (Objects.equals(indexTypeEnum, IndexTypeEnum.IDX_SAAS_INVOICE_EXPRESS) || Objects.equals(indexTypeEnum, IndexTypeEnum.IDX_SAAS_PURCHASE_INVOICE_EXPRESS)) {
            subFormData = paasFormDataEntity.getSingleSubFormData();
         } else {
            subFormData = paasFormDataEntity.getSubFormData();
        }
        JSONObject data = paasFormDataEntity.getData();
        if (Objects.isNull(subFormData)) {
            return;
        }
        if (CollectionsUtil.isEmpty(insertList)) {
            return;
        }
        Map<String, ? extends List<? extends PaasFormSubDataEntity>> map = insertList.stream().collect(Collectors.groupingBy(PaasFormSubDataEntity::getAttr));
        Set<Map.Entry<String, Object>> entrySet = subFormData.entrySet();
        Iterator<Map.Entry<String, Object>> iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            String key = entry.getKey();
            Object valueArrayObj = entry.getValue();
            JSONArray valueArray = new JSONArray();
            if (valueArrayObj instanceof JSONArray) {
                valueArray = (JSONArray)valueArrayObj;
            }else if (valueArrayObj instanceof List) {
                valueArray = JSONArray.parseArray(JSON.toJSONString((List)valueArrayObj));
            }
            Iterator<Object> valueIterator = valueArray.iterator();
            while (valueIterator.hasNext()) {
                Object valueJsonObj = valueIterator.next();
                JSONObject valueJson = new JSONObject();
                if (valueJsonObj instanceof  JSONObject) {
                    valueJson = (JSONObject)valueJsonObj;
                }else {
                    valueJson = JSONObject.parseObject(JSON.toJSONString(valueJsonObj));
                }
                Long subDataId = valueJson.getLong(BusinessConstant.SUB_DATA_ID);
                if (Objects.isNull(subDataId)) {
                    valueIterator.remove();
                }
            }
            List<? extends PaasFormSubDataEntity> paasFormSubDataEntities = map.get(key);
            if (CollectionsUtil.isNotEmpty(paasFormSubDataEntities)) {
                JSONArray shouldAddJsonArray = new JSONArray();
                paasFormSubDataEntities.forEach(a -> {
                    JSONObject shouldJson = a.getData();
                    shouldJson.put(BusinessConstant.SUB_DATA_ID, a.getId());
                    shouldAddJsonArray.add(shouldJson);
                });
                valueArray.addAll(shouldAddJsonArray);
            }
        }
    }

    private UpdateFormatSubDataDTO formatUpdateSubData(PaasFormDataEntity paasFormDataEntity, List<? extends PaasFormSubDataEntity> paasFormPaasSubDataEntities, JSONObject subFormData) {
        UpdateFormatSubDataDTO updateFormatSubDataDTO = new UpdateFormatSubDataDTO();
        List<PaasFormSubDataEntity> deleteList = new ArrayList<>();
        List<PaasFormSubDataEntity> insertList = new ArrayList<>();
        List<UpdateDataEntity> updateList = new ArrayList<>();
        List<PaasFormSubDataEntity> updateDelList = new ArrayList<>();
        String corpid = paasFormDataEntity.getCorpid();
        Map<Long, PaasFormSubDataEntity> subMap = new HashMap<>();
        paasFormPaasSubDataEntities.forEach(v -> subMap.put(v.getId(), v));

        subFormData.forEach((k, v) -> {
            JSONArray jsonArray = JSON.parseArray(JSON.toJSONString(v, SerializerFeature.WriteMapNullValue,SerializerFeature.DisableCircularReferenceDetect));
            jsonArray.forEach(item -> {
                JSONObject jsonObject = (JSONObject) item;
                Long id = jsonObject.getLong(BusinessConstant.SUB_DATA_ID);
                if (Objects.isNull(id)) {
                    insertList.add(new PaasFormSubDataEntity(paasFormDataEntity.getFormId(), corpid, paasFormDataEntity.getId(), k, jsonObject));
                } else {
                    if (subMap.containsKey(id)) {
                        // data内id不存储,移除
                        jsonObject.remove(BusinessConstant.SUB_DATA_ID);
                        if (!jsonObject.isEmpty()) {
                            UpdateDataEntity updateData = ExplainUtil.getUpdateData(id, jsonObject, corpid);
                            PaasFormSubDataEntity paasFormSubDataEntity = subMap.get(id);
                            if (Objects.equals(DelEnum.APPROVE_FAILED.getDel(), paasFormSubDataEntity.getDel())) {
                                paasFormSubDataEntity.setDel(DelEnum.NORMAL.getDel());
                                updateDelList.add(paasFormSubDataEntity);
                            }
                            // 子表单更新需要设置parentId
                            updateData.setParentId(paasFormDataEntity.getId());
                            updateList.add(updateData);
                        }
                        subMap.remove(id);
                    }
                }
            });
        });

        subMap.forEach((k, v) -> {
            deleteList.add(v);
        });

        updateFormatSubDataDTO.setDeleteList(deleteList);
        updateFormatSubDataDTO.setInsertList(insertList);
        updateFormatSubDataDTO.setUpdateList(updateList);
        updateFormatSubDataDTO.setUpdateDelList(updateDelList);
        return updateFormatSubDataDTO;

    }


    @Override
    public void deleteByDataId(Long dataId, String corpid, IndexTypeEnum indexTypeEnum) throws XbbException {
        switch (indexTypeEnum) {
            case IDX_SUB_FORM_DATA:
                paasFormPaasSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_CUSTOMER_SUB:
            case IDX_SAAS_CUSTOMER_SUB_SPECIAL:
                customerSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_CONTRACT_SUB:
                contractSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_QUOTATION_SUB:
                quotationSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_OUTSTOCK_SUB:
                outstockSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_CONTACT_SUB:
                contactSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_INVOICE_SUB:
                invoiceSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_INVOICE_EXPRESS:
                invoiceExpressSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_PURCHASE_INVOICE_SUB:
                purchaseInvoiceSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_PURCHASE_INVOICE_EXPRESS:
                purchaseInvoiceExpressSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_INSTOCK_SUB:
                instockSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_ASSEMBLE_SUB:
                assembleSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_PAYMENT_SUB:
                paymentSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_PAYMENT_SHEET_SUB:
                paymentSheetSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_PAYMENT_TASK_SUB:
                paymentTaskSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_PAYPLAN_SHEET_SUB:
                payPlanSheetSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_PRODUCTION_ORDER_SUB:
                productionOrderSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_PAY_PLAN_SUB:
                payPlanSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_COMMUNICATE_PLAN_SUB:
                communicatePlanSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_OPPORTUNITY_SUB:
                opportunitySubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_BOM_SUB:
                bomBillSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_CUSTOMER_COMMUNICATE_SUB:
                customerCommunicateSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_SUPPLIER_COMMUNICATE_SUB:
                supplierCommunicateSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_WORK_REPORT_SUB:
                workReportSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_PURCHASE_SUB:
                purchaseSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_RETURNED_PURCHASE_SUB:
                returnedPurchaseSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_SUPPLIER_CONTACT_SUB:
                supplierContactSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_SUPPLIER_SUB:
                supplierSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_PRODUCT_SUB:
                productSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_INVENTORY_SUB:
                inventorySubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_TRANSFER_SUB:
                transferSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_WAREHOUSE_SUB:
                warehouseSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_REFUND_SUB:
                refundSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_WORK_ORDER_SUB:
                workOrderSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_CLUE_SUB:
                clueSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_MARKET_ACTIVITY_SUB:
                marketActivitySubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_CREDIT_TEMPORARY_LIMIT_SUB:
                creditTemporaryLimitSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_CREDIT_FIXED_LIMIT_SUB:
                creditFixedLimitSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_OTHER_INCOME_SUB:
                otherIncomeSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_OTHER_EXPENSE_SUB:
                otherExpenseSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SAAS_FUND_TRANSFER_SUB:
                fundTransferSubDataModel.deleteByDataId(dataId, corpid);
                break;
            case IDX_SERVICE_PROJECT_SUB:
                paasFormPaasSubDataDao.deleteByDataId(dataId, corpid, SubFormTableNameConstant.TB_WORK_ORDER_V2_SERVICE_PROJECT_SUB_DATA);
            default:
                break;
        }
    }

    @Override
    public void deleteBatchByDataId(List<Long> dataIds, String corpid, IndexTypeEnum indexTypeEnum, DelEnum delEnum) throws XbbException {
        if (CollectionsUtil.isEmpty(dataIds)) {
            return;
        }
        switch (indexTypeEnum) {
            case IDX_SUB_FORM_DATA:
                paasFormPaasSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_CUSTOMER_SUB:
            case IDX_SAAS_CUSTOMER_SUB_SPECIAL:
                customerSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_CONTRACT_SUB:
                contractSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_QUOTATION_SUB:
                quotationSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_OUTSTOCK_SUB:
                outstockSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_CONTACT_SUB:
                contactSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_INVOICE_SUB:
                invoiceSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_INVOICE_EXPRESS:
                invoiceExpressSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_PURCHASE_INVOICE_SUB:
                purchaseInvoiceSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_PURCHASE_INVOICE_EXPRESS:
                purchaseInvoiceExpressSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_INSTOCK_SUB:
                instockSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_ASSEMBLE_SUB:
                assembleSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_PAYMENT_SUB:
                paymentSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_PAYMENT_SHEET_SUB:
                paymentSheetSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_PAYMENT_TASK_SUB:
                paymentTaskSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_PAYPLAN_SHEET_SUB:
                payPlanSheetSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_PRODUCTION_ORDER_SUB:
                productionOrderSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_PAY_PLAN_SUB:
                payPlanSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_COMMUNICATE_PLAN_SUB:
                communicatePlanSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_OPPORTUNITY_SUB:
                opportunitySubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_BOM_SUB:
                bomBillSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_CUSTOMER_COMMUNICATE_SUB:
                customerCommunicateSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_SUPPLIER_COMMUNICATE_SUB:
                supplierCommunicateSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_WORK_REPORT_SUB:
                workReportSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_PURCHASE_SUB:
                purchaseSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_RETURNED_PURCHASE_SUB:
                returnedPurchaseSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_SUPPLIER_CONTACT_SUB:
                supplierContactSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_SUPPLIER_SUB:
                supplierSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_PRODUCT_SUB:
                productSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_INVENTORY_SUB:
                inventorySubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_TRANSFER_SUB:
                transferSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_WAREHOUSE_SUB:
                warehouseSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_REFUND_SUB:
                refundSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_WORK_ORDER_SUB:
                workOrderSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_CLUE_SUB:
                clueSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_MARKET_ACTIVITY_SUB:
                marketActivitySubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_CREDIT_TEMPORARY_LIMIT_SUB:
                creditTemporaryLimitSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_CREDIT_FIXED_LIMIT_SUB:
                creditFixedLimitSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_OTHER_INCOME_SUB:
                otherIncomeSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_OTHER_EXPENSE_SUB:
                otherExpenseSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SAAS_FUND_TRANSFER_SUB:
                fundTransferSubDataModel.deleteBatchByDataId(dataIds, corpid, delEnum);
                break;
            case IDX_SERVICE_PROJECT_SUB:
                deleteBatchByDataId4WorkOrderV2(dataIds, corpid, delEnum, indexTypeEnum, SubFormTableNameConstant.TB_WORK_ORDER_V2_SERVICE_PROJECT_SUB_DATA);
                break;
            case IDX_RECEIPT_ORDER_SUB:
                deleteBatchByDataId4WorkOrderV2(dataIds, corpid, delEnum, indexTypeEnum, SubFormTableNameConstant.TB_WORK_ORDER_V2_RECEIPT_SUB_DATA);
                break;
            case IDX_WORK_ORDER_V2_SUB:
                deleteBatchByDataId4WorkOrderV2(dataIds, corpid, delEnum, indexTypeEnum, SubFormTableNameConstant.TB_WORK_ORDER_V2_SUB_DATA);
                break;
            default:
                break;
        }
    }

    private void deleteBatchByDataId4WorkOrderV2(List<Long> dataIds, String corpid, DelEnum delEnum, IndexTypeEnum indexTypeEnum, String tableName) throws XbbException {
        List<? extends PaasFormSubDataEntity> entities = paasFormPaasSubDataDao.getByDataIds(dataIds, corpid, delEnum.getDel(), tableName);
        paasFormPaasSubDataDao.deleteBatchByDataId(dataIds, corpid, tableName, delEnum.getDel());
        paasFormSubDataEsModel.deleteBatch(corpid, entities, indexTypeEnum, WriteRequest.RefreshPolicy.NONE, DelEnum.DELETE_SUBFORM);
    }

    @Override
    public void restoreBatch(List<Long> dataIds, String corpid, IndexTypeEnum indexTypeEnum) throws XbbException {
        switch (indexTypeEnum) {
            case IDX_SUB_FORM_DATA:
                paasFormPaasSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_CUSTOMER_SUB:
            case IDX_SAAS_CUSTOMER_SUB_SPECIAL:
                customerSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_CONTRACT_SUB:
                break;
            case IDX_SAAS_QUOTATION_SUB:
                quotationSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_OUTSTOCK_SUB:
                break;
            case IDX_SAAS_CONTACT_SUB:
                contactSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_INVOICE_SUB:
                invoiceSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_INVOICE_EXPRESS:
                invoiceExpressSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_PURCHASE_INVOICE_SUB:
                break;
            case IDX_SAAS_PURCHASE_INVOICE_EXPRESS:
                break;
            case IDX_SAAS_INSTOCK_SUB:
                break;
            case IDX_SAAS_ASSEMBLE_SUB:
                break;
            case IDX_SAAS_PAYMENT_SUB:
                break;
            case IDX_SAAS_PAYMENT_SHEET_SUB:
                paymentSheetSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_PAYMENT_TASK_SUB:
                break;
            case IDX_SAAS_PAYPLAN_SHEET_SUB:
                payPlanSheetSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_PRODUCTION_ORDER_SUB:
                productionOrderSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_PAY_PLAN_SUB:
                payPlanSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_COMMUNICATE_PLAN_SUB:
                communicatePlanSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_OPPORTUNITY_SUB:
                opportunitySubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_BOM_SUB:
                bomBillSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_CUSTOMER_COMMUNICATE_SUB:
                customerCommunicateSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_SUPPLIER_COMMUNICATE_SUB:
                supplierCommunicateSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_WORK_REPORT_SUB:
                workReportSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_PURCHASE_SUB:
                purchaseSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_RETURNED_PURCHASE_SUB:
                returnedPurchaseSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_SUPPLIER_CONTACT_SUB:
                supplierContactSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_SUPPLIER_SUB:
                supplierSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_PRODUCT_SUB:
                productSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_INVENTORY_SUB:
                inventorySubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_TRANSFER_SUB:
                transferSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_WAREHOUSE_SUB:
                warehouseSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_REFUND_SUB:
                refundSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_WORK_ORDER_SUB:
                workOrderSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_CLUE_SUB:
                clueSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_MARKET_ACTIVITY_SUB:
                marketActivitySubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_CREDIT_TEMPORARY_LIMIT_SUB:
                break;
            case IDX_SAAS_CREDIT_FIXED_LIMIT_SUB:
                break;
            case IDX_SAAS_OTHER_INCOME_SUB:
                otherIncomeSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_OTHER_EXPENSE_SUB:
                otherExpenseSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SAAS_FUND_TRANSFER_SUB:
                fundTransferSubDataModel.restoreBatch(dataIds, corpid);
                break;
            case IDX_SERVICE_PROJECT_SUB:
                paasFormPaasSubDataDao.restoreBatch(dataIds, corpid, SubFormTableNameConstant.TB_WORK_ORDER_V2_SERVICE_PROJECT_SUB_DATA);
                break;
            default:
                break;
        }
    }

    @Override
    public List<? extends PaasFormSubDataEntity> getListByIdAndAttr(Long id, String corpid, IndexTypeEnum indexTypeEnum, String attr) throws XbbException {
        return getListByIdAndAttr( id,  corpid,  indexTypeEnum,  attr, null , null);
    }

    @Override
    public List<? extends PaasFormSubDataEntity> getListByIdAndAttr(Long id, String corpid, IndexTypeEnum indexTypeEnum, String attr, Integer from, Integer pageSize) throws XbbException {
        Map<String, Object> map = new HashMap<>(1 << 4);
        map.put("corpid", corpid);
        map.put("dataId", id);
        map.put("del", DelEnum.NORMAL.getDel());
        map.put("attr", attr);
        map.put("start",from);
        map.put("pageNum",pageSize);
        return getSubDataList(map, indexTypeEnum);
    }

    @Override
    public Integer getCountByIdAndAttr(Long id, String corpid, IndexTypeEnum indexTypeEnum, String attr) throws XbbException {
        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid", corpid);
        map.put("dataId", id);
        map.put("del", DelEnum.NORMAL.getDel());
        map.put("attr", attr);
        Integer count = 0;
        IModel iModel = getModel(indexTypeEnum);
        if(iModel != null){
            count = iModel.getEntitysCount(map);
        }
        return count;
    }

    @Override
    public List<? extends PaasFormSubDataEntity> getByIdList(List<Long> idList, String corpid, IndexTypeEnum indexTypeEnum) throws XbbException {
        if (CollectionsUtil.isEmpty(idList)) {
            return new ArrayList<>();
        }
        Map<String, Object> map = new HashMap<>(1 << 4);
        map.put("corpid", corpid);
        map.put("idIn", idList);
        map.put("del", DelEnum.NORMAL.getDel());
        return getSubDataList(map, indexTypeEnum);
    }

    @Override
    public List<? extends PaasFormSubDataEntity> getByDataIdList(List<Long> idList, Integer del, String corpid, IndexTypeEnum indexTypeEnum) throws XbbException {
        if (CollectionsUtil.isEmpty(idList)) {
            return new ArrayList<>();
        }
        Map<String, Object> map = new HashMap<>(1 << 4);
        map.put("corpid", corpid);
        map.put("dataIdIn", idList);
        map.put("del", del);
        return getSubDataList(map, indexTypeEnum);
    }

    /**
     * 根据indexTypeEnum获取子表单数据
     * @param map 查询参数
     * @param indexTypeEnum indexTypeEnum
     * @return List<? extends PaasFormSubDataEntity>
     */
    @Override
    public List<? extends PaasFormSubDataEntity> getSubDataList(Map<String, Object> map, IndexTypeEnum indexTypeEnum) {
        List<? extends PaasFormSubDataEntity> subDataEntities = new ArrayList<>();
        switch (indexTypeEnum) {
            case IDX_SUB_FORM_DATA:
                subDataEntities = paasFormPaasSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_CUSTOMER_SUB:
            case IDX_SAAS_CUSTOMER_SUB_SPECIAL:
                subDataEntities = customerSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_CONTRACT_SUB:
                subDataEntities = contractSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_QUOTATION_SUB:
                subDataEntities = quotationSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_OUTSTOCK_SUB:
                subDataEntities = outstockSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_CONTACT_SUB:
                subDataEntities = contactSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_INVOICE_SUB:
                subDataEntities = invoiceSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_INVOICE_EXPRESS:
                subDataEntities = invoiceExpressSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_PURCHASE_INVOICE_SUB:
                subDataEntities = purchaseInvoiceSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_PURCHASE_INVOICE_EXPRESS:
                subDataEntities = purchaseInvoiceExpressSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_INSTOCK_SUB:
                subDataEntities = instockSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_ASSEMBLE_SUB:
                subDataEntities = assembleSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_PAYMENT_SUB:
                subDataEntities = paymentSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_PAYMENT_SHEET_SUB:
                subDataEntities = paymentSheetSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_PAYMENT_TASK_SUB:
                subDataEntities = paymentTaskSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_PAYPLAN_SHEET_SUB:
                subDataEntities = payPlanSheetSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_PRODUCTION_ORDER_SUB:
                subDataEntities = productionOrderSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_PAY_PLAN_SUB:
                subDataEntities = payPlanSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_COMMUNICATE_PLAN_SUB:
                subDataEntities = communicatePlanSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_OPPORTUNITY_SUB:
                subDataEntities = opportunitySubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_BOM_SUB:
                subDataEntities = bomBillSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_CUSTOMER_COMMUNICATE_SUB:
                subDataEntities = customerCommunicateSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_SUPPLIER_COMMUNICATE_SUB:
                subDataEntities = supplierCommunicateSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_WORK_REPORT_SUB:
                subDataEntities = workReportSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_PURCHASE_SUB:
                subDataEntities = purchaseSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_RETURNED_PURCHASE_SUB:
                subDataEntities = returnedPurchaseSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_SUPPLIER_CONTACT_SUB:
                subDataEntities = supplierContactSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_SUPPLIER_SUB:
                subDataEntities = supplierSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_PRODUCT_SUB:
                subDataEntities = productSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_INVENTORY_SUB:
                subDataEntities = inventorySubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_TRANSFER_SUB:
                subDataEntities = transferSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_WAREHOUSE_SUB:
                subDataEntities = warehouseSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_REFUND_SUB:
                subDataEntities = refundSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_WORK_ORDER_SUB:
                subDataEntities = workOrderSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_CLUE_SUB:
                subDataEntities = clueSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_MARKET_ACTIVITY_SUB:
                subDataEntities = marketActivitySubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_CREDIT_TEMPORARY_LIMIT_SUB:
                subDataEntities = creditTemporaryLimitSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_CREDIT_FIXED_LIMIT_SUB:
                subDataEntities = creditFixedLimitSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_OTHER_INCOME_SUB:
                subDataEntities = otherIncomeSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_OTHER_EXPENSE_SUB:
                subDataEntities = otherExpenseSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_FUND_TRANSFER_SUB:
                subDataEntities = fundTransferSubDataModel.findEntitys(map);
                break;
            case IDX_SAAS_COST_ADJUST_SUB:
                subDataEntities = costAdjustSubDataModel.findEntitys(map);
                break;
            case IDX_SERVICE_PROJECT_SUB:
                subDataEntities = paasFormPaasSubDataModel.findEntitys4WorkOrderV2(map, SubFormTableNameConstant.TB_WORK_ORDER_V2_SERVICE_PROJECT_SUB_DATA);
                break;
            case IDX_WORK_ORDER_V2_SUB:
                subDataEntities = paasFormPaasSubDataModel.findEntitys4WorkOrderV2(map, SubFormTableNameConstant.TB_WORK_ORDER_V2_SUB_DATA);
                break;
            case IDX_RECEIPT_ORDER_SUB:
                subDataEntities = paasFormPaasSubDataModel.findEntitys4WorkOrderV2(map, SubFormTableNameConstant.TB_WORK_ORDER_V2_RECEIPT_SUB_DATA);
                break;
            case IDX_SAAS_COMPETITOR_SUB:
                subDataEntities = competitorSubDataModel.findEntitys(map);
                break;
            default:
                break;
        }
        return subDataEntities;
    }

    IModel getModel(IndexTypeEnum indexTypeEnum){
        IModel iModel = null;
        switch (indexTypeEnum) {
            case IDX_SUB_FORM_DATA:
                iModel = paasFormPaasSubDataModel;
                break;
            case IDX_SAAS_CUSTOMER_SUB:
            case IDX_SAAS_CUSTOMER_SUB_SPECIAL:
                iModel = customerSubDataModel;
                break;
            case IDX_SAAS_CONTRACT_SUB:
                iModel = contractSubDataModel;
                break;
            case IDX_SAAS_QUOTATION_SUB:
                iModel = quotationSubDataModel;
                break;
            case IDX_SAAS_OUTSTOCK_SUB:
                iModel = outstockSubDataModel;
                break;
            case IDX_SAAS_CONTACT_SUB:
                iModel = contactSubDataModel;
                break;
            case IDX_SAAS_INVOICE_SUB:
                iModel = invoiceSubDataModel;
                break;
            case IDX_SAAS_INVOICE_EXPRESS:
                iModel = invoiceExpressSubDataModel;
                break;
            case IDX_SAAS_PURCHASE_INVOICE_SUB:
                iModel = purchaseInvoiceSubDataModel;
                break;
            case IDX_SAAS_PURCHASE_INVOICE_EXPRESS:
                iModel = purchaseInvoiceExpressSubDataModel;
                break;
            case IDX_SAAS_INSTOCK_SUB:
                iModel = instockSubDataModel;
                break;
            case IDX_SAAS_ASSEMBLE_SUB:
                iModel = assembleSubDataModel;
                break;
            case IDX_SAAS_PAYMENT_SUB:
                iModel = paymentSubDataModel;
                break;
            case IDX_SAAS_PAYMENT_SHEET_SUB:
                iModel = paymentSheetSubDataModel;
                break;
            case IDX_SAAS_PAYMENT_TASK_SUB:
                iModel = paymentTaskSubDataModel;
                break;
            case IDX_SAAS_PAYPLAN_SHEET_SUB:
                iModel = payPlanSheetSubDataModel;
                break;
            case IDX_SAAS_PRODUCTION_ORDER_SUB:
                iModel = productionOrderSubDataModel;
                break;
            case IDX_SAAS_PAY_PLAN_SUB:
                iModel = payPlanSubDataModel;
                break;
            case IDX_SAAS_COMMUNICATE_PLAN_SUB:
                iModel = communicatePlanSubDataModel;
                break;
            case IDX_SAAS_OPPORTUNITY_SUB:
                iModel = opportunitySubDataModel;
                break;
            case IDX_SAAS_BOM_SUB:
                iModel = bomBillSubDataModel;
                break;
            case IDX_SAAS_CUSTOMER_COMMUNICATE_SUB:
                iModel = customerCommunicateSubDataModel;
                break;
            case IDX_SAAS_SUPPLIER_COMMUNICATE_SUB:
                iModel = supplierCommunicateSubDataModel;
                break;
            case IDX_SAAS_WORK_REPORT_SUB:
                iModel = workReportSubDataModel;
                break;
            case IDX_SAAS_PURCHASE_SUB:
                iModel = purchaseSubDataModel;
                break;
            case IDX_SAAS_RETURNED_PURCHASE_SUB:
                iModel = returnedPurchaseSubDataModel;
                break;
            case IDX_SAAS_SUPPLIER_CONTACT_SUB:
                iModel = supplierContactSubDataModel;
                break;
            case IDX_SAAS_SUPPLIER_SUB:
                iModel = supplierSubDataModel;
                break;
            case IDX_SAAS_PRODUCT_SUB:
                iModel = productSubDataModel;
                break;
            case IDX_SAAS_INVENTORY_SUB:
                iModel = inventorySubDataModel;
                break;
            case IDX_SAAS_TRANSFER_SUB:
                iModel = transferSubDataModel;
                break;
            case IDX_SAAS_WAREHOUSE_SUB:
                iModel = warehouseSubDataModel;
                break;
            case IDX_SAAS_REFUND_SUB:
                iModel = refundSubDataModel;
                break;
            case IDX_SAAS_WORK_ORDER_SUB:
                iModel = workOrderSubDataModel;
                break;
            case IDX_SAAS_CLUE_SUB:
                iModel = clueSubDataModel;
                break;
            case IDX_SAAS_MARKET_ACTIVITY_SUB:
                iModel = marketActivitySubDataModel;
                break;
            case IDX_SAAS_CREDIT_TEMPORARY_LIMIT_SUB:
                iModel = creditTemporaryLimitSubDataModel;
                break;
            case IDX_SAAS_CREDIT_FIXED_LIMIT_SUB:
                iModel = creditFixedLimitSubDataModel;
                break;
            case IDX_SAAS_OTHER_INCOME_SUB:
                iModel = otherIncomeSubDataModel;
                break;
            case IDX_SAAS_OTHER_EXPENSE_SUB:
                iModel = otherExpenseSubDataModel;
                break;
            case IDX_SAAS_FUND_TRANSFER_SUB:
                iModel = fundTransferSubDataModel;
                break;
            default:
                break;
        }
        return iModel;
    }


    @Override
    public void updateBatch(List<UpdateDataEntity> updateList, String corpid, IndexTypeEnum indexTypeEnum) throws XbbException {
        if (updateList.isEmpty() || Objects.isNull(corpid) || Objects.isNull(indexTypeEnum)) {
            return;
        }
        switch (indexTypeEnum) {
            case IDX_SUB_FORM_DATA:
                paasFormPaasSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_CUSTOMER_SUB:
            case IDX_SAAS_CUSTOMER_SUB_SPECIAL:
                customerSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_CONTRACT_SUB:
                contractSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_QUOTATION_SUB:
                quotationSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_OUTSTOCK_SUB:
                outstockSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_CONTACT_SUB:
                contactSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_INVOICE_SUB:
                invoiceSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_INVOICE_EXPRESS:
                invoiceExpressSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_PURCHASE_INVOICE_SUB:
                purchaseInvoiceSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_PURCHASE_INVOICE_EXPRESS:
                purchaseInvoiceExpressSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_INSTOCK_SUB:
                instockSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_ASSEMBLE_SUB:
                assembleSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_PAYMENT_SUB:
                paymentSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_PAYMENT_SHEET_SUB:
                paymentSheetSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_PAYPLAN_SHEET_SUB:
                payPlanSheetSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_PRODUCTION_ORDER_SUB:
                productionOrderSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_PAY_PLAN_SUB:
                payPlanSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_COMMUNICATE_PLAN_SUB:
                communicatePlanSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_OPPORTUNITY_SUB:
                opportunitySubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_BOM_SUB:
                bomBillSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_CUSTOMER_COMMUNICATE_SUB:
                customerCommunicateSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_SUPPLIER_COMMUNICATE_SUB:
                supplierCommunicateSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_WORK_REPORT_SUB:
                workReportSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_PURCHASE_SUB:
                purchaseSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_RETURNED_PURCHASE_SUB:
                returnedPurchaseSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_SUPPLIER_CONTACT_SUB:
                supplierContactSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_SUPPLIER_SUB:
                supplierSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_PRODUCT_SUB:
                productSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_INVENTORY_SUB:
                inventorySubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_TRANSFER_SUB:
                transferSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_WAREHOUSE_SUB:
                warehouseSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_REFUND_SUB:
                refundSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_WORK_ORDER_SUB:
                workOrderSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_CLUE_SUB:
                clueSubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SAAS_MARKET_ACTIVITY_SUB:
                marketActivitySubDataModel.updateBatch(updateList, corpid);
                break;
            case IDX_SERVICE_PROJECT_SUB:
                paasFormPaasSubDataDao.updateBatch(updateList, corpid, SubFormTableNameConstant.TB_WORK_ORDER_V2_SERVICE_PROJECT_SUB_DATA);
                break;
            default:
                break;
        }
    }

    @Override
    public Integer updateBatch(List<? extends PaasFormSubDataEntity> list, IndexTypeEnum indexTypeEnum, String corpid) throws XbbException {
        if (CollectionsUtil.isEmpty(list) || Objects.isNull(corpid) || Objects.isNull(indexTypeEnum)) {
            return 0;
        }
        Integer num = 0;
        switch (indexTypeEnum) {
            case IDX_SUB_FORM_DATA:
                num = paasFormPaasSubDataModel.updateBatchByEntity(list, corpid);
                break;
            // CRM
            case IDX_SAAS_CUSTOMER_SUB:
            case IDX_SAAS_CUSTOMER_SUB_SPECIAL:
                num = customerSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_CONTRACT_SUB:
                num = contractSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_CONTACT:
                num = contactSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_QUOTATION_SUB:
                num = quotationSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_REFUND_SUB:
                num = refundSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_OPPORTUNITY_SUB:
                num = opportunitySubDataModel.updateBatchByEntity(list, corpid);
                break;

                // 市场管理
            case IDX_SAAS_CLUE_SUB:
                num = clueSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_MARKET_ACTIVITY_SUB:
                num = marketActivitySubDataModel.updateBatchByEntity(list, corpid);
                break;
                // 资金管理
            case IDX_SAAS_PAYPLAN_SHEET_SUB:
                num = payPlanSheetSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_PAYMENT_SHEET_SUB:
                num = paymentSheetSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_PURCHASE_INVOICE_SUB:
                num = purchaseInvoiceSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_PURCHASE_INVOICE_EXPRESS:
                num = purchaseInvoiceExpressSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_INVOICE_SUB:
                num = invoiceSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_INVOICE_EXPRESS:
                num = invoiceExpressSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_FUND_TRANSFER_SUB:
                num = fundTransferSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_OTHER_EXPENSE_SUB:
                num = otherExpenseSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_OTHER_INCOME_SUB:
                num = otherIncomeSubDataModel.updateBatchByEntity(list, corpid);
                break;
                // 进销存
            case IDX_SAAS_PURCHASE_SUB:
                num = purchaseSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_RETURNED_PURCHASE_SUB:
                num = returnedPurchaseSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_SUPPLIER_SUB:
                num = supplierSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_SUPPLIER_COMMUNICATE_SUB:
                num = supplierCommunicateSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_SUPPLIER_CONTACT_SUB:
                num = supplierContactSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_ASSEMBLE_SUB:
                num = assembleSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_BOM_SUB:
                num = bomBillSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_INSTOCK_SUB:
                num = instockSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_OUTSTOCK_SUB:
                num = outstockSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_PRODUCTION_ORDER_SUB:
                num = productionOrderSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_TRANSFER_SUB:
                num = transferSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_INVENTORY_SUB:
                num = inventorySubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_WAREHOUSE_SUB:
                num = warehouseSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_WORK_ORDER_SUB:
                num = workOrderSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_COST_ADJUST_SUB:
                num = costAdjustSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SAAS_COMPETITOR_SUB:
                num = competitorSubDataModel.updateBatchByEntity(list, corpid);
                break;
            case IDX_SERVICE_PROJECT_SUB:
                num = paasFormPaasSubDataDao.updateBatchByEntity(list, corpid, SubFormTableNameConstant.TB_WORK_ORDER_V2_SERVICE_PROJECT_SUB_DATA);
                paasFormSubDataEsModel.updateBatchByEntity(list, IndexTypeEnum.IDX_SERVICE_PROJECT_SUB, WriteRequest.RefreshPolicy.NONE);
                break;
            case IDX_WORK_ORDER_V2_SUB:
                num = paasFormPaasSubDataDao.updateBatchByEntity(list, corpid, SubFormTableNameConstant.TB_WORK_ORDER_V2_SUB_DATA);
                paasFormSubDataEsModel.updateBatchByEntity(list, IndexTypeEnum.IDX_WORK_ORDER_V2_SUB, WriteRequest.RefreshPolicy.NONE);
                break;
            case IDX_RECEIPT_ORDER_SUB:
                num = paasFormPaasSubDataDao.updateBatchByEntity(list, corpid, SubFormTableNameConstant.TB_WORK_ORDER_V2_RECEIPT_SUB_DATA);
                paasFormSubDataEsModel.updateBatchByEntity(list, IndexTypeEnum.IDX_RECEIPT_ORDER_SUB, WriteRequest.RefreshPolicy.NONE);
                break;
            default:
                break;
        }
        return num;
    }

    @Override
    public Map<Long, JSONObject> getSubDataMap(String corpid, Integer saasMark, Integer businessType, Long formId, Collection<Long> parentIdList, Integer page, Integer pageSize, Integer del) throws XbbException {
        Map<Long, JSONObject> formDataMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (CollectionsUtil.isEmpty(parentIdList)) {
            return formDataMap;
        }
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.CORPID, corpid);
        param.put(StringConstant.DEL, del);
        param.put("dataIdIn", parentIdList);
        param.put(StringConstant.FORM_ID, formId);
        IndexTypeEnum indexTypeEnum = IndexTypeEnum.getSubEnum(indexTypeModel.getIndexType(corpid, businessType, saasMark));
        List<? extends  PaasFormSubDataEntity> formSubDataList = getSubDataList(param, indexTypeEnum);
        if (CollectionsUtil.isNotEmpty(formSubDataList)) {
            formSubDataList.forEach(item->{
                JSONObject jsonObject = new JSONObject();
                if (formDataMap.containsKey(item.getDataId())) {
                    jsonObject = formDataMap.get(item.getDataId());
                }
                if (jsonObject.containsKey(item.getAttr())) {
                    JSONArray jsonArray = jsonObject.getJSONArray(item.getAttr());
                    jsonArray.add(item.getData());
                    jsonObject.put(item.getAttr(), jsonArray);
                } else {
                    JSONArray jsonArray = new JSONArray();
                    jsonArray.add(item.getData());
                    jsonObject.put(item.getAttr(), jsonArray);
                }
                formDataMap.put(item.getDataId(), jsonObject);
            });
        }
        return formDataMap;
    }

    /**
     * 获取paas子表单最大id
     *
     * @param params
     * @return
     * @throws XbbException
     */
    @Override
    public Long getMaxId(Map<String, Object> params) throws XbbException {
        return paasFormPaasSubDataModel.getMaxId(params);
    }
}
